package com.leetcode.p0;

import com.bean.TreeNode;
import com.utils.TreeUtils;

import java.util.Deque;
import java.util.LinkedList;

/**
 * 中序遍历
 */
public class Test98_zhongxu {

    public boolean isValidBST(TreeNode root) {
        Deque<TreeNode> stack = new LinkedList<>();

        double inorder = -Double.MAX_VALUE;

        while (!stack.isEmpty() || root != null) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }

            root = stack.poll();
            if (root.val <= inorder) {
                return false;
            }
            inorder = root.val;

            root = root.right;
        }
        return true;
    }

    public static void main(String[] args) {
        TreeNode treeNode = TreeUtils.buildTree4();
//        TreeNode treeNode = TreeUtils.buildTree("1,1");
        TreeUtils.showTree(treeNode);

        Test98_zhongxu test = new Test98_zhongxu();
        boolean result = test.isValidBST(treeNode);
        System.out.println(result);
    }

}
